package com.fjwt.gz.coupon.yspay.yspayutil;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.PublicKey;
import java.security.Signature;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class SignUtil {

    private static final String ALGORITHM = "SHA256withRSA";

    /**
     * @Author
     * @Description 验签
     * @Date 10:38 2020/3/12
     * @Param
     * @return
     */
    public static boolean signVerify(Map<String, Object> map, String signData,PublicKey publicKey)throws Exception {
        byte[] srcData = getSignDataStr(map).getBytes("utf-8");
        return validateSignBySoft(publicKey, org.apache.commons.codec.binary.Base64.decodeBase64(signData), srcData);
    }

    public static boolean validateSignBySoft(PublicKey publicKey,byte[] signData, byte[] srcData) throws Exception {
        Signature st = Signature.getInstance(ALGORITHM);
        st.initVerify(publicKey);
        st.update(srcData);
        return st.verify(signData);
    }

    public static String getSignDataStr(Map<String, Object> map) {
        List<String> keys = new ArrayList<String>(map.keySet());
        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        for(String key : keys){
            if("sign".equals(key)) {
                continue;
            }
            sb.append(key).append("=");
            sb.append(map.get(key));
            sb.append("&");
        }
        if(sb.length() > 0) {
            sb.setLength(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String getSignDataStr1(Map<String, String> map) {
        List<String> keys = new ArrayList<String>(map.keySet());
        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        for(String key : keys){
            if("sign".equals(key)) {
                continue;
            }
            if("$jacocoData".equals(key)) {
                continue;
            }
            sb.append(key).append("=");
            sb.append(map.get(key));
            sb.append("&");
        }
        if(sb.length() > 0) {
            sb.setLength(sb.length() - 1);
        }
        return sb.toString();
    }

    /**
     * 加密
     *
     * @param content 需要加密的内容
     * @param password  加密密钥
     * @return
     */
    public static byte[] encrypt(byte[] content, byte[] password) throws Exception {
        try {
            SecretKeySpec key = new SecretKeySpec(password,"AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(content);
            return result; // 加密
        } catch (Exception e) {
            throw e;
        }
    }


    /**解密
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, byte[] password) throws Exception {
        try {
            SecretKeySpec key = new SecretKeySpec(password,"AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(content);
            return result; // 解密
        } catch (Exception e) {
            throw e;
        }
    }
}
